<?php
namespace AuctionBack\Service\Thrift;

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Protocol\TMultiplexedProtocol;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
use Thrift\Transport\THttpClient;
use Services\AuctionService\AuctionServiceClient;

class ThriftAuctionService implements ThriftClientIf
{
    protected $AuctionService;

    public function __construct($app_url = '')
    {
        if (empty($app_url)) {
            $app_url = C('ANT_NEST.WEAVER_URL');
        }
        $handle = new THttpClient($app_url, 80, '/Auction/run');
        $transport = new TBufferedTransport($handle);
        $protocol = new TBinaryProtocol($transport);
        $WalletProtocol = new TMultiplexedProtocol($protocol, 'AuctionService');
        $this->AuctionService = new AuctionServiceClient($WalletProtocol);
    }

    /**
     *
     * 创建拍卖场次
     * @param array $inParamsCreateEditScene
     * @return bool|ErrorArray
     *
     */
    public function createEditScene($inParamsCreateEditScene)
    {
        try {
            $result = $this->AuctionService->createEditScene($inParamsCreateEditScene);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'msg' => $tx->getMessage());
        }
    }


    /**
     *
     * 上拍
     * @param \Services\AuctionService\inParamsCreateOrder $inParamsCreateOrder
     * @return array|WalletList
     *
     */
    public function createOrder($inParamsCreateOrder)
    {
        try {
            $result = $this->AuctionService->createOrder($inParamsCreateOrder);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'msg' => $tx->getMessage());
        }
    }


    /**
     *
     * 上拍审核
     *
     * @param integer orderId 被审核的拍单ID
     * @param bool result 审核结果(true 通过 false 驳回)
     * @param string reason 驳回原因
     * @param integer empId 审核者
     * @return  bool
     *
     */
    public function auditOrder($orderId, $result, $reason, $empId)
    {
        try {
            $result = $this->AuctionService->auditOrder($orderId, $result, $reason, $empId);
            return $result;
        } catch (TException $tx) {
            return array('code' => $tx->getCode(), 'msg' => $tx->getMessage());
        }
    }
}